<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/12 0012
 * Time: 14:41
 */
namespace gaodeapi;
use app\admin\model\Logistical;
use app\lib\dadaapi\ToolsInco;
use think\Controller;


class gaoDeApi extends Controller
{
    //高德所支持的经纬度转换类型
    const BAIDU = "baidu";
    const MAPBAR = "mapbar";
    const GPS = "gps";
    const AUTONAVI = "autonavi";

    /**
     * 发送请求,POST
     * @param $url 指定URL完整路径地址
     * @param $data 请求的数据
     */
    public function getHttpRequestWithPost($url,$data){
        // json
        $headers = array(
            'Content-Type: application/json',
        );
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_TIMEOUT, 3);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $resp = curl_exec($curl);
        //var_dump( curl_error($curl) );//如果在执行curl的过程中出现异常，可以打开此开关查看异常内容。
        $info = curl_getinfo($curl);
        curl_close($curl);
        if (isset($info['http_code']) && $info['http_code'] == 200) {
            return $resp;
        }
        return '';
    }

    /**
     * 发送请求,GET
     * @param $url 指定URL完整路径地址
     * @param $data 请求的数据
     */
    public function getHttpRequestWithGet($url){
        // json
        $headers = array(
            'Content-Type: application/json',
        );
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 3);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $resp = curl_exec($curl);
        //var_dump( curl_error($curl) );//如果在执行curl的过程中出现异常，可以打开此开关查看异常内容。
        $info = curl_getinfo($curl);
        curl_close($curl);
        if (isset($info['http_code']) && $info['http_code'] == 200) {
            return $resp;
        }
        return '';
    }



    /**
     * @param $data
     * @return string
     * 高德参数序列化
     */
    private function gaodeParamSerialize($data){
        $tools = new ToolsInco();
        $config = $tools->getGaodeConfig();
        $data['key']=$config['key'];
        $data['output']="json";

        $paramStr = "";
        foreach ($data as $key=>$item){
            $paramStr = $paramStr.$key.'='.$item.'&';
        }
        return $paramStr;
    }


    private function gaodeParamSer($data){
        $paramStr = "";
        foreach ($data as $key=>$item){
            $paramStr = $paramStr.$key.'='.$item.'&';
        }
        return $paramStr;
    }


    //
    public function baiduTransformationGaode($gaodeAddressGeo,$Transformation){
        $gaodeConfigService = new ToolsInco();
        $gaodeConfig = $gaodeConfigService->getGaodeConfig();
        $key = $gaodeConfig['key'];
        $param['key']=$key;
        $param['coordsys']=$Transformation;
        $param['locations'] = $gaodeAddressGeo;
        $param['output'] = "json";

        $param = $this->gaodeParamSerialize($param);

        $url = 'http://restapi.amap.com/v3/assistant/coordinate/convert';

        $result = $this->getHttpRequestWithPost($url,$param);
        $result = json_decode($result);

        if($result->status == 1){

            //status 1表示成功  0表示失败
            return $result->locations;

        }else{
            return false;
        }
    }

    public function bicycling($origin,$destination){
        $ToolsInco = new ToolsInco();
        $gaodeConfig = $ToolsInco->getGaodeConfig();
        $data['key'] = $gaodeConfig['key'];
        $data['origin'] = $origin;
        $data['destination'] = $destination;
        $param = self::gaodeParamSer($data);
        $url = "http://restapi.amap.com/v4/direction/bicycling?".$param;
        $result = $this->getHttpRequestWithPost($url,$param);
        $result = json_decode($result,true);
        if(isset($result['errcode']) && $result['errcode']!=0){
            $error['status']=Logistical::STATUS_INVALID;
            $error['errcode']=$result['errcode'];
            $error['message'] = $result['errdetail'];
            $error['errmsg'] = $result['errmsg'];
            return $error;
        }
        $district = $result['data']['paths'][0]['distance'];

        $paramData['status']=Logistical::STATUS_VALID;
        $paramData['district']=$district;

        return $paramData;
    }

    /**
     * 高德地图
     * 获取用户地址详细信息,包括经纬度
     * 罗俊阳
     */
    public function getAddres($data){

        $tools = new ToolsInco();
        $config = $tools->getGaodeConfig();
        $data['key']=$config['key'];
        $data['output']="json";

        $paramStr = "";

        foreach ($data as $key=>$item){
            $paramStr = $paramStr.$key."=".$item."&";
        }

        $url = "http://restapi.amap.com/v3/geocode/geo";
        $res = $this->getHttpRequestWithPost($url,$paramStr);

        return $res;
    }



}